package sketch.ounit.fuzz;

import java.util.LinkedList;
import java.util.List;

import randoop.ComponentManager;
import randoop.ExecutableSequence;
import randoop.RConstructor;
import randoop.RMethod;
import randoop.Sequence;
import randoop.main.GenInputsAbstract;
import randoop.main.GenTests;
import sketch.specs.RandomSequenceCreator;
import sketch.util.Checker;

public class SequencePool {
	//all fields, and methods
	public final List<Class<?>> classes_under_test = new LinkedList<Class<?>>();
	public final int time_limit;
	public final ComponentManager component_manager;
	public final RandomSequenceCreator creator;
	public SequencePool(int time_limit, Class<?>... classes ) {
		Checker.checkTrue(classes.length > 0, "The classes could not be empty.");
		for(Class<?> clz : classes) {
			Checker.checkNull(clz, "The class can not be null.");
			this.classes_under_test.add(clz);
		}
		Checker.checkNull(time_limit > 0, "The time limit : " + time_limit + " is illegal.");
		this.time_limit = time_limit;
		
		//create sequence
		this.creator = new RandomSequenceCreator(classes[0], classes);
		creator.generateSequences(this.time_limit);
		this.component_manager = creator.getComponentManagerInRandoop();
	}
	
	RandomSequenceCreator getRandomCreator() {
		return this.creator;
	}
	int size() {
		return this.component_manager.numGeneratedSequences();
	}
	
	public List<RConstructor> lookup_constructors(Class<?> clz) {
		return creator.lookup_constructors(clz);
	}
	
	public List<RMethod> lookup_methods(Class<?> clz) {
		return creator.lookup_rmethods(clz);
	}
	
	public void output_randoop_sequences() {
	    List<ExecutableSequence> eseq_list = new LinkedList<ExecutableSequence>();
	    for(Sequence seq : component_manager.getAllGeneratedSequences()) {
		    ExecutableSequence eseq = new ExecutableSequence(seq);
		    eseq.execute(null);
		    eseq_list.add(eseq);
	    }
	    GenTests.write_junit_tests(GenInputsAbstract.junit_output_dir , eseq_list, null);
	}
}